package com.jx.gn.handler;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jx.gn.config.CommParams;
import com.jx.gn.config.LinkdoodProperties;
import com.jx.gn.entity.linkdood.Message;
import com.jx.gn.entity.linkdood.ReqComm;
import com.jx.gn.service.UserService;
import com.jx.gn.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * 豆豆消息发送
 */
@Component
@Slf4j
public class LinkdoodHandler {

    @Autowired
    private LinkdoodProperties linkdoodProperties;
    @Autowired
    private UserService userService;

    /**
     * 发送语音消息到豆豆平台
     *
     * @return
     */
    public String sendVoice(File file, WxMpXmlMessage wxMessage) {
        //上传文件
        JSONObject fileRes = sendFile(file, CommParams.UPLOAD_FILE_VOICE);
        //发送语音消息
        if (fileRes.getInt("code") == 0) {
            log.info("上传语音成功 res{}", fileRes.toString());
            JSONObject result = fileRes.getJSONObject("result");

            Message msg = new Message();
            msg.setMediaUrl(result.getJSONObject("resourceRemark").getStr("url"));
            msg.setDocid(0);
            msg.setFileName(result.getStr("resourceName"));
            ReqComm<Message> req = new ReqComm<>();
            req.setReceTargetID(getIdCard(wxMessage));
            req.setChatScene("user");
            req.setMessageType(6);
            req.setMessage(msg);
            String s = HttpClientUtil.doPostJson(linkdoodProperties.getSenMsgUrl(), JSONUtil.toJsonStr(req));
            log.info("发送语音消息成功 {}", s);
            JSONObject jsonObject = JSONUtil.parseObj(s);
            if (jsonObject.getInt("code") != 0) {
                log.info("发送文件消息失败 {}", s);
            }
        }
        return null;
    }

    /**
     * @param file
     * @param wxMessage
     */
    public void sendImage(File file, WxMpXmlMessage wxMessage) {
        //上传文件
        JSONObject fileRes = sendFile(file, CommParams.UPLOAD_FILE_IMG);
        if (fileRes.getInt("code") == 0) {
            //发送图片消息
            log.info("上传图片成功 res{}", fileRes.toString());
            JSONObject result = fileRes.getJSONObject("result");
            Message msg = new Message();
            msg.setMediaUrl(result.getJSONObject("resourceRemark").getStr("url"));
            msg.setDocid(0);
            msg.setFileName(result.getStr("resourceName"));
            msg.setThumbUrl(result.getJSONObject("resourceRemark").getStr("url"));

            ReqComm<Message> req = new ReqComm<>();
            req.setReceTargetID(getIdCard(wxMessage));
            req.setChatScene("user");
            req.setMessageType(5);
            req.setMessage(msg);
            String s = HttpClientUtil.doPostJson(linkdoodProperties.getSenMsgUrl(), JSONUtil.toJsonStr(req));
            log.info("发送图片消息成功 {}", s);
            JSONObject jsonObject = JSONUtil.parseObj(s);
            if (jsonObject.getInt("code") != 0) {
                log.info("发送图片消息失败 {}", s);
            }
        } else {
            log.info("上传图片异常{}", fileRes.toString());
        }

    }

    /**
     * 想逗逗发送文本消息
     *
     * @param wxMessage
     */
    public void sendText(WxMpXmlMessage wxMessage) {
        ReqComm<String> req = new ReqComm<>();
        req.setReceTargetID(getIdCard(wxMessage));
        req.setChatScene("user");
        req.setMessageType(2);
        req.setMessage(wxMessage.getContent());
        String s = HttpClientUtil.doPostJson(linkdoodProperties.getSenMsgUrl(), JSONUtil.toJsonStr(req));
        log.info("发送文本消息 {}", s);
        JSONObject jsonObject = JSONUtil.parseObj(s);
        if (jsonObject.getInt("code") != 0) {
            log.info("发送文本消息失败 {}", s);
        }
    }


    public JSONObject sendFile(File file, String type) {
        return JSONUtil.parseObj(HttpClientUtil.doPostFile(linkdoodProperties.getUploadUrl(), file, type));
    }


    public void sendHtml(WxMpXmlMessage wxMessage) {
        //1.发送url 到python   python下载mhtml文件 返回文件路径
        String url =linkdoodProperties.python+wxMessage.getContent();
        String fileUrl = HttpClientUtil.doGet(url);
        log.info(fileUrl);
        if(StrUtil.isNotBlank(fileUrl)){
            sendVoice(new File(fileUrl),wxMessage);
        }else{
            log.info("python 没有文件目录 wx{}",wxMessage);
        }

    }
    private String getIdCard(WxMpXmlMessage wxMessage){
        return userService.getUserByWxId(wxMessage.getFromUser()).getIdCard();
    }
}
